import BuffUtil from '@/BuffUtil';
import RandomUtil from '@/RandomUtil';
import UnitStateUtil from '@/UnitStateUtil';
import SelectCommon from 'code/Core/Common/SelectCommon';
import UnitCommon from 'code/Core/Common/UnitCommon';
import TalentBase from 'code/Scripts/Abilities/base/TalentBase';
const desc = `你的攻击会降低敌人的攻速和移速,同时附加${ColorSafe}法强x0.33|r的冰霜伤害，并在攻击时有${ColorSafe}15%|r的机率对周围${ColorSafe}450|r范围内的敌人施加霜之哀伤。|n觉醒效果：受到霜之哀伤影响的敌人死亡时会回复你最大生命的${ColorSafe}13%|r`;
const art = 'assets\\icon\\Spell_DeathKnight_FrozenRuneWeapon.blp';
const name = '霜之哀伤';
const config: TalentConfigModel = {
    id: 'talent_t_TalentSadOfForst',
    name,
    desc,
    art,
    buffType: {
        id: 'buff_t_TalentSadOfForst',
        art,
        name,
        ubertip: desc,
    },
    buffTypePlus: {
        id: 'buff_t_TalentSadOfForst_plus',
        art,
        name: '霜这哀伤[觉醒]',
        ubertip: `你的攻击会降低敌人的攻速和移速,同时附加${ColorSafe}法强x0.33|r的冰霜伤害，并在攻击时有${ColorSafe}15%|r的机率对周围${ColorSafe}450|r范围内的敌人施加霜之哀伤，且当受到霜之哀伤影响的敌人死亡时会回复你最大生命的${ColorSafe}13%|r`,
    },
};

const effectBuffType: AppBuffType = {
    id: 'buff_t_TalentSadOfForst_effect',
    art,
    name: '霜这哀伤',
    ubertip: '受到霜之哀伤的攻击，攻速与移速下降',
    isDebuff: true,
    stackCountMax: 3,
    dur: 5,
    attribute: {
        attackSpd_p: -0.12,
        move_speed_p: -0.08,
    },
    targetArt: 'Abilities\\Spells\\Other\\FrostDamage\\FrostDamage.mdl',
    targetAttach: 'origin',
};

export default class TalentSadOfForst extends TalentBase {
    static instance: TalentSadOfForst;
    constructor(p?: TalentConfigModel) {
        super(p);
        TalentSadOfForst.instance = this;
        this.setTalentConfig(config);
        this.init();
        EventManager.onTakeAttack(this.attackEffect);
        EventManager.单位死亡(this.deathEffect);
    }
    deathEffect(d: DamageEventData) {
        let _this = TalentSadOfForst.instance;
        let { plus } = _this.talentUnEffect(d.source);
        if (!plus) return;
        let dmg = UnitStateUtil.getMaxLife(d.source) * 0.13;
        DamageSystemInstance.applyHeal(d.source, d.source, dmg);
        DestroyEffect(AddSpecialEffectTarget('Abilities\\Spells\\Orc\\FeralSpirit\\feralspiritdone.mdl', d.source, 'origin'));
    }
    applyDebuff(tar: unit, id: string, source: unit) {
        BuffUtil.addBuff(tar, id, source);
        let dmg = UnitCommon.取法强(source) * 0.33;
        DamageSystemInstance.applyIceDamage(tar, source, dmg);
    }
    attackEffect(d: DamageEventData) {
        if (d.isAttach) return;
        let _this = TalentSadOfForst.instance;
        let { unEffect, plus } = _this.talentUnEffect(d.source);
        if (unEffect) return;
        let id = effectBuffType.id;
        _this.applyDebuff(d.target, id, d.source);
        let r = RandomUtil.randomPercent();
        if (r < 0.15) {
            let es = SelectCommon.getAllyUnitInRangeExcludeCenterUnit(d.target, 450);
            for (let e of es) {
                _this.applyDebuff(e, id, d.source);
            }
        }
    }
    init() {
        BuffUtil.registerBuffType(effectBuffType);
    }
}
